import { creatureDies } from '$lib/death';
import { Types, defineComponent, type IWorld, type Entity } from '$lib/ecs';
import { logPlayer } from '$lib/player';

export const BloodLevel = defineComponent('Blood', {
  max: Types.i32,
  amount: Types.i32,
});

export const getBloodLevel = (world, e) => {
  return {
    max: BloodLevel.max[e],
    amount: BloodLevel.amount[e],
  };
};

export const loseBlood = (world: IWorld, e: Entity, amount: number) => {
  const blood = BloodLevel.amount[e] - amount;
  BloodLevel.amount[e] = blood;
  if (blood <= 0) {
    logPlayer(world, e, "You lost too much blood!");
    creatureDies(world, e);
  }
};
